
function del_nodes(id_name){
	var f = document.getElementById(id_name); 
	var childs = f.childNodes;
for(var i = childs.length - 1; i >= 0; i--) { 
	// if (childs[i].nodeName=="IMG"){
  f.removeChild(childs[i]); 
}
};

function add_imgs(imgs){
	del_nodes('dd')
	var dd = document.getElementById("dd");
	dd.style.width="750px";
		if (imgs.length>0){
		for(var i=0;i<imgs.length;i++){
	         if (imgs[i].match('http')){
			var im = document.createElement("img");
				im.setAttribute("src",imgs[i]);
				 im.style.maxWidth="96%";
				 im.style.border="3px solid black";
				// im.setAttribute("width",100);
			var ck = document.createElement("input");
				ck.setAttribute("type","checkbox");
				ck.setAttribute("name","cimg");
				ck.setAttribute("value",imgs[i]);
				dd.appendChild(im);
				dd.appendChild(ck);
			}}}

	// var bt = document.createElement("input");
	// bt.setAttribute("type","submit");
	// bt.setAttribute("id","down");
	// dd.appendChild(bt);
};

var get_img = document.getElementById("get_img"); 
get_img.onclick=function(){ 
chrome.tabs.getSelected(null, function (tab) {
    chrome.tabs.sendMessage(tab.id, 'get_img', function(response) {
    	console.log(response)
        if (response){
        add_imgs(response.split("|"));
    }
    	else{
		alert("No imgs,reload and try again!");
	}
    });
})};

var down_img = document.getElementById("down"); 
down_img.onclick=function(){ 
img=document.forms[0].cimg;
for (i=0;i<img.length;++ i){
	if (img[i].checked){
		chrome.downloads.download({url:img[i].value,conflictAction:"uniquify",saveAs:false});
		}
	}
};
var get_cookies = document.getElementById("get_cookies"); 
get_cookies.onclick=function(cookies){ 
chrome.tabs.getSelected(function(tab) {
    url=tab.url;
    domin=url.split('.')[1];
    console.log(url);
    console.log(domin);
    chrome.cookies.getAll({},function(cookies){
    	var dic = {};
    	for (i=0;i<cookies.length;++ i){
	        if(cookies[i].domain.match(domin)){
	        	dic[cookies[i].name]=cookies[i].value;
	        } };
 alert(JSON.stringify(dic));
});
});
}
